import { getAppInfo } from '@/lib/api/app';
import { getImgServerUrl } from '@/lib/api/server';
import { AppDetail } from '@/types/app';
import { createResource } from 'solid-js';


const fetchAppDetail = async (category: string, pkgname: string): Promise<AppDetail> => {  
  try {
    const appInfo = await getAppInfo(category, pkgname);
    
    // 生成5张截图的链接
    const screenshots = Array.from({ length: 5 }, (_, index) => 
      `${getImgServerUrl()}/${appInfo.Category}/${appInfo.Pkgname}/screen_${index}.png`
    );
    
    return {
      ...appInfo,
      Screenshots: screenshots
    };
  } catch (error) {
    console.error(`获取应用详情失败: ${error}`);
    throw error;
  }
};

export const useAppDetailStore = (category: string, pkgname: string) => {
  const [app, { refetch }] = createResource<AppDetail>(() => fetchAppDetail(category, pkgname));
  const loading = () => app.loading;

  return {
    app,
    loading,
    refetch,
  };
};